<?php
class SignController{
	private $_parameters;
	public $user;
	
	function __construct($__parameters=null){
		$this->_parameters = explode(';',$__parameters);
	}
	
	public function indexAction($__parameters=null){
		global $objSession,$smarty, $user,$config;		
		$config->__logg = $objSession->IsLoggedIn();
		switch ($this->_parameters[0]){
			case 'login':
				$objSession->Login($config->nazwa_uzytkownika,$config->haslo);
				$config->__logg = $objSession->IsLoggedIn();
				if( $config->__logg ){				
					$smarty->assign_by_ref('username',$user->nazwa_uzytkownika);
					
					$smarty->display("section/const_logout.tpl");
					
					if ( $user->stanowisko == 'a')	$smarty->display("section/admin/const_url_ustawienia_administratora.tpl");
					if ( $user->stanowisko == 'u' || $user->stanowisko == 'p' || $user->stanowisko == 'f' ){
						$smarty->assign('userid',$user->id);
						$smarty->display("section/admin/const_url_ustawienia_uzytkownika.tpl");
					}elseif ( $user->stanowisko == 'b') {
						echo ' | zablokowany';	
					}
					if($config->__isset('url') && $config->url != ''){
						tools::_redirect($config->url,'Ładowanie strony, proszę czekać...');
						exit;
					}
					
				}else{
					$smarty->assign('error','Nieudane logowanie, spróbuj jeszcze raz.');
					$smarty->display("section/const_login.tpl");
				}
				break;
			
			default:
				if( $config->__logg ){
					$smarty->assign_by_ref('username',$user->nazwa_uzytkownika);
					$smarty->display("section/const_logout.tpl");

					if ( $user->stanowisko == 'a')	$smarty->display("section/admin/const_url_ustawienia_administratora.tpl");
					if ( $user->stanowisko == 'u' || $user->stanowisko == 'p' || $user->stanowisko == 'f'){
						$smarty->assign('userid',$user->id);
						$smarty->display("section/admin/const_url_ustawienia_uzytkownika.tpl");
					}elseif ( $user->stanowisko == 'b'){
						echo ' | zablokowany';	
					}
				}else{
					$smarty->display("section/const_login.tpl");
				}
		}
		//unset($_REQUEST);
	}

	public function logoutAction(){
		global $objSession;
		$objSession->LogOut();
	}

	public function signAction(){
		global $objSession;
		SignController::SignIn($this->_parameters[0],$objSession);
	}
	
	public static function SignIn($par=null,user_session $sess__){
		global $user,$smarty,$config,$dispatcher;
		try{	
			switch($par){
				case 'login':
					$sess__->Login($config->nazwa_uzytkownika,$config->haslo);
					$config->__logg = $sess__->IsLoggedIn();
					if( $config->__logg ){				
						$user = $sess__->GetUserObject();	
						if($config->__isset('url') && $config->url != ''){
							tools::_redirect($config->url,"",0,"php");
							exit;
						}else{					
							tools::_redirect("/","",0,"php");
						}
					}else{
						throw new Exception("Błąd logowania...");
						$user = null;
					}
					break;
					
				case 'logout':
					$sess__->LogOut();
					$config->__logg = 0;
					$user = null;
					tools::_redirect("/","",0,"php");
					break;	
	
				default:
					if( $config->__logg ){
						$user = $sess__->GetUserObject();						
					}else{
						$user = null;
						$config->__logg = 0;
					}
			}
			return $user;
		}catch (Exception $e) {
		   $dispatcher->Panel_Komunikaty->msg = $e->getMessage();
		}
	}

}